package com.fruit.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;

import net.sf.json.JSONObject;

public class HttpClientManager {

	public static String getData(HttpGet httpget) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();

		String res = null;
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpget);
			InputStream in = response.getEntity().getContent();
			res = IOUtils.toString(in);
			in.close();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res;
	}

	public static String getData(String url) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();

		HttpGet httpget = new HttpGet(url);

		String res = null;
		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpget);
			InputStream in = response.getEntity().getContent();
			res = IOUtils.toString(in);
			in.close();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res;
	}

	public static String token(String clientid,String clientSecret,String orgName,String appName) {

		String path = "http://a1.easemob.com/"+orgName+"/"+appName+"/token";

		Map<String, Object> map = new HashMap<>();
		map.put("grant_type", "client_credentials");
		map.put("client_id", clientid);
		map.put("client_secret", clientSecret);

		String json = CustomUtil.mapToJson(map);
		// System.out.println(json);
		String result = getToken(path, json);
		JSONObject jsonObject = JSONObject.fromObject(result);
		Map<String, Object> mapJson = JSONObject.fromObject(jsonObject);
		String token = (String) mapJson.get("access_token");
		return token;
	}

	public static String getToken(String url, String xmlParam) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();

		HttpPost httpPost = new HttpPost(url);

		String res = null;
		CloseableHttpResponse response = null;
		try {
			httpPost.setEntity(new StringEntity(xmlParam, "UTF-8"));
			response = httpClient.execute(httpPost);

			InputStream in = response.getEntity().getContent();
			res = IOUtils.toString(in);
			in.close();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res;
	}

	public static String postData(String url, String xmlParam, String token) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();

		HttpPost httpPost = new HttpPost(url);
		//HttpDelete httpDelete = new HttpDelete(url);

		String res = null;
		CloseableHttpResponse response = null;
		try {
			if (token != null && token != "") {
				httpPost.addHeader("Authorization", "Bearer " + token);
				httpPost.addHeader("Content-Type", "application/json");
			}
			httpPost.setEntity(new StringEntity(xmlParam, "UTF-8"));
			response = httpClient.execute(httpPost);
			InputStream in = response.getEntity().getContent();
			res = IOUtils.toString(in);
			in.close();// https://a1.easemob.com/hitx/hltxchat/users/hltx18926bef88c0d2909391322ea913fc6d/password
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res;

	}

	public static String deleteAddFriend(String url, String token) {
		CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();

		//HttpPost httpPost = new HttpPost(url);
		HttpDelete httpDelete = new HttpDelete(url);
		String res = null;
		CloseableHttpResponse response = null;
		try {
			httpDelete.addHeader("Authorization", "Bearer "+token);
			// httpPost.addHeader("Content-Type","application/json");
			// ((HttpResponse) httpDelete).setEntity(new StringEntity(xmlParam, "UTF-8"));
			response = httpClient.execute(httpDelete);

			InputStream in = response.getEntity().getContent();
			res = IOUtils.toString(in);
			in.close();
		} catch (UnsupportedOperationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return res;

	}

	public static String HxRegister(Map<String, Object> userMap,String orgName,String appName,String clientid,String clientSecret) {
		String addUserPath = "https://a1.easemob.com/"+orgName+"/"+appName+"/users";
		String res = postData(addUserPath, CustomUtil.mapToJson(userMap), token(clientid,clientSecret,orgName,appName));
		System.out.println(res);
		return res;

	}
	public static String deleteHxUser(String userName) {
		
		/*String addUserPath = "https://a1.easemob.com/hitx/hltxchat/users/"+ userName;
		//String res = deleteAddFriend(addUserPath, token(clientid,clientSecret,orgName,appName));
		System.out.println(res);*/
		String res = "";
		return res;
		
	}
	

	public static String updateHxPwd(String userName, Map<String, Object> userMap,String clientid,String clientSecret,String orgName,String appName) {

		String addUserPath = "https://a1.easemob.com/"+orgName+"/"+appName+"/users/" + userName + "/password";
		String res = postData(addUserPath, CustomUtil.mapToJson(userMap), token(clientid,clientSecret,orgName,appName));
		System.out.println(res);
		return res;

	}

//	public static String addFriend(String myName, String friend) {
//
////		String path = "http://a1.easemob.com/hitx/hltxchat/users/" + myName + "/contacts/users/" + friend + "";
////		//String res = postAddFriend(path);
////		System.out.println(res);
////		JSONObject json = JSONObject.fromObject(res);
////		Map<String, Object> mapJson = JSONObject.fromObject(json);
////		String str = (String) mapJson.get("entities");
////		return str;
//	}

}
